import pandas as pd
from pyecharts import Line

# 导入 bitcoin数据 BCHAIN-MKPRU.csv
bcc_df = pd.read_csv('BCHAIN-MKPRU.csv')

# 打印预览
print(bcc_df)

# 导入 gold数据 LBMA-GOLD.csv
gold_df = pd.read_csv('LBMA-GOLD.csv')

# 打印预览
print(gold_df)

# 合并两项数据
data = pd.merge(bcc_df, gold_df, on='Date', how='outer')
print("data==============================", data)

# 补全gold数据中因为周六周日不开市而产生的空白值，使用前一行数据，对于第一行的数据无法使用向前补全，转为使用向后补全
data["USD (PM)"].fillna(method='ffill', inplace=True)
data["USD (PM)"].fillna(method='ffill', inplace=True)
data["USD (PM)"].fillna(method='bfill', inplace=True)

# 展示处理好的数据
print("data(After filled)==============================\n", data, "data counts:", data.count())

# bcc_df.append(gold_df,ignore_index=True)

# print(bcc_df)

# 两种商品的相关性检验
print("spearman秩相关", data.corr('spearman'))

# 画出对应的两个折线图
line_bitcoin = Line("Bitcoin")
line_gold = Line("Gold")
line_bitcoin.add("bitcoin price", data['Date'], data['Value'], is_label_show=True, yaxis_min=-5, yaxis_max=70000,
          yaxis_name="price(USD)/BTC", yaxis_name_gap=50, is_toolbox_show=True)
line_gold.add("gold price", data['Date'], data['USD (PM)'], is_label_show=True, yaxis_min=1000, yaxis_max=2100,
          yaxis_name="USD/ounce(oz)", yaxis_name_gap=50, is_toolbox_show=True)
line_bitcoin.render(path='./1.bitcoin.html')
line_gold.render(path='./2.gold.html')
